public class LeetCode83 {
    static class ListNode{
        int val;
        ListNode next;
        ListNode(){}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public static ListNode deleteDuplicates(ListNode head){
        if (head != null && head.next != null){
            ListNode lastNode = head;
            ListNode node = head.next;
            while (node != null){
                if (node.val == lastNode.val){

                    lastNode.next = null;

                    if (node.next != null){
                        ListNode nodeTemp = node.next;
                        node.next = null;
                        node = nodeTemp;
                        lastNode.next = node;
                    }else {
                        node = null;
                    }
                }else {
                    lastNode = node;
                    node = node.next;
                }
            }

//            return lastNode;
        }

        return head;
    }
}
